//author:jxx
import store from "@/store/index";
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
    components: {//动态扩充组件或组件路径
        //表单header、content、footer对应位置扩充的组件
        gridHeader: () => import("./distributor/product.vue"),//{ template: "<div>扩展组xx件</div>" },
        gridBody: '',
        gridFooter: '',
        //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
        modelHeader: '',
        modelBody: '',
        modelFooter: ''
    },
    buttons: { view: [], box: [], detail: [] },//扩展的按钮
    methods: {//事件扩展
        getOption (field) {
            let option;
            this.editFormOptions.forEach(x => {
                x.forEach(item => {
                    if (item.field == field) {
                        option = item;
                    }
                })
            })
            return option;
        },
        onInit () {
            //新建窗口的宽和高
            this.boxOptions.width = 1000;
            this.boxOptions.height = 600;
            //    单选、不显示复选框、显示序号
            //;
            this.textInline = true;
            //;
            //;
            // let distributorName = this.getOption("distributorName");
            // let distributorCode = this.getOption("distributorCode");
            let spare10 = this.getOption("spare10");
            let smsId = this.getOption("smsId");
            smsId.onChange = (val, option) => {
                // console.log(option)
                spare10.hidden = false;
                let data = option.data.filter(item => item.key == val);
                if (data.length > 0) { this.editFormFields[spare10.field] = data[0].remark; } else { spare10.hidden = true }
            }
            // distributorName.onInput = (val) => {
            //     this.editFormFields[distributorCode.field] = this.base.chineseToPinYin(val)
            // }

            //   添加操作列
            this.columns.push({
                title: '操作',
                fixed: 'right',
                width: 150,
                render: (h, { row, column, index }) => {
                    if (row.distributorType == "2002") {
                        return h(
                            "div", { style: {} }, [
                            // h(
                            //     "a", {
                            //     props: {},
                            //     style: {},
                            //     on: {
                            //         click: (e) => {
                            //             this.$router.push({ name: "MallStoreNavigation", params: { distributorCode: row.distributorCode } });
                            //             localStorage.setItem("distributorId", row.distributorCode)
                            //             // localStorage.getItem("distributorId"）
                            //         }
                            //     }
                            // },
                            //     "商城装修"
                            // ),
                            h(
                                "a", {
                                props: {},
                                style: { "margin-left": "10px" },
                                on: {
                                    click: (e) => {
                                        this.$refs.gridHeader.open(row)
                                    }
                                }
                            },
                                "产品发布"
                            ),
                        ])
                    }
                }
            })
            let accountsId = this.getOption('accountsId')
            let param = {
                accName: '',
                accCode: '',
                customer: '',
                phone: '',
                isvalid: null,
                start: 0,
                count: 1000,
                Credit: 1,
            };
            this.http.post('/api/AccountsMain/AccountsGet', param).then((res) => {
                let list = [];
                res.data.accounts.forEach((o) => {
                    let obj = {
                        value: o.accMain.account_name,
                        key: o.accMain.account_code_fk,
                    };
                    list.push(obj);
                    this.$set(accountsId, 'data', list);
                });
            });
            this.getMenu();
        },
        searchBefore (param) { //查询ViewGird表数据前,param查询参数
            //添加其他查询条件,也可以在后台扩展中写查询条件
            // console.log(param, 'param')
            let wheres = {
                'name': 'distributorType',
                'value': '2002',
                'displayType': 'checkbox'
            }
            param.wheres.push(wheres)
            return true;
        },
        rowClick ({ row, column, event }) { //查询界面table点击行选中当前行
            this.$refs.table.$refs.table.toggleRowSelection(row);
            return true;
        },
        direct () {
            let _userInfo = store.getters.getUserInfo();
            let distributorCode = this.getOption("distributorCode");
            let distributorName = this.getOption("distributorName");
            let productType = this.getOption("productType");
            let accountsId = this.getOption("accountsId");
            let isMessage = this.getOption("isMessage");
            let smsId = this.getOption("smsId");
            let smsMode = this.getOption("smsMode");
            let signCode = this.getOption("signCode");//短信签名
            let contactPerson = this.getOption("contactPerson");
            let contactPhone = this.getOption("contactPhone");
            let timeContainer = this.getOption("timeContainer");//起止时间
            // let distributorEndTime = this.getOption("distributorEndTime");
            // let distributorStartTime = this.getOption("distributorStartTime");
            let travelAgentsPerson = this.getOption('travelAgentsPerson')
            distributorCode.hidden = false;
            distributorCode.required = true;
            productType.hidden = false;
            accountsId.hidden = false;
            accountsId.required = true;
            distributorName.hidden = false;
            distributorName.required = true;
            isMessage.hidden = false;
            if (this.editFormFields[isMessage.field] == 1) {
                smsId.required = true;
                smsMode.required = true;
                signCode.required = true;
            }
            smsId.hidden = false;
            smsMode.hidden = false;
            signCode.hidden = false;
            contactPerson.hidden = false;
            //contactPerson.required=true;
            contactPhone.hidden = false;
            contactPhone.required=true;
            timeContainer.hidden = false
            timeContainer.required = true;
            travelAgentsPerson.hidden = false
            // distributorEndTime.hidden = false;
            // distributorEndTime.required = true;
            // distributorStartTime.hidden = false;
            // distributorStartTime.required = true;
        },
        async modelOpenBeforeAsync (row) {
            let smsId = this.getOption("smsId");
            let smsMode = this.getOption("smsMode");
            let signCode = this.getOption("signCode");
            let spare10 = this.getOption("spare10");
            let distributorType = this.getOption("distributorType");
            let isMessage = this.getOption("isMessage");
            // 2002 旅行社
            // 2004 直营
            // 2005 渠道
            // 2006 直营分销
            if (row.distributorType == 2002) {
                this.direct();
                this.editFormFields.accountsId = row.accountsId.toString()
            }
            distributorType.onChange = (val, option) => {
                if (val == 2002) {
                    this.direct();
                }
            }
            if (this.editFormFields[isMessage.field] == 1 || row.isMessage == 1) {
                smsId.hidden = false;
                smsMode.hidden = false;
                signCode.hidden = false;
                smsId.required = true;
                smsMode.required = true;
                signCode.required = true;
            } else {
                this.editFormFields[signCode.field] = "";
                this.editFormFields[smsId.field] = "";
                this.editFormFields[smsMode.field] = "";
                smsId.hidden = true;
                smsMode.hidden = true;
                signCode.hidden = true;
                spare10.hidden = true;
                smsId.required = false;
                smsMode.required = false;
                signCode.required = false;
            }
            isMessage.onChange = (val, option) => {
                if (val == 1) {
                    smsId.hidden = false;
                    smsMode.hidden = false;
                    signCode.hidden = false;
                    smsId.required = true;
                    smsMode.required = true;
                    signCode.required = true;
                } else {
                    this.editFormFields[signCode.field] = "";
                    this.editFormFields[smsId.field] = "";
                    this.editFormFields[smsMode.field] = "";
                    smsId.hidden = true;
                    smsMode.hidden = true;
                    signCode.hidden = true;
                    spare10.hidden = true;
                    smsId.required = false;
                    smsMode.required = false;
                    signCode.required = false;
                }
            }
            return true;
        },
        // 点击编辑/新建按钮弹出框后
        async modelOpenAfter (row) {
            if (this.currentAction == "update") {
                this.editFormFields.timeContainer = [row.distributorStartTime, row.distributorEndTime]
            } else {
                // console.log("新增")
                let distributorName = this.getOption("distributorName");
                let distributorCode = this.getOption("distributorCode");
                distributorName.onInput = (val) => {
                    this.editFormFields[distributorCode.field] = this.base.chineseToPinYin(val)
                }
            }
            let distributorType = this.getOption("distributorType");
            await this.http.post("/api/Dictionary/GetVueDictionary", ["distributor_Type"], true).then(res => {
                let arr = []
                res[0].data.forEach(item => {
                    if (item.key == 2002) {
                        arr.push(item)
                    }
                })
                this.$set(distributorType, "data", arr)
            })
            return true
        },
        addBefore (formData) { //新建保存前formData为对象，包括明细表
            return this.veriFromTable(formData)
        },
        updateBefore (formData) { //编辑保存前formData为对象，包括明细表、删除行的Id
            return this.veriFromTable(formData)
        },
        // 保存前编辑
        veriFromTable (formData) {
            console.log("保存前编辑", formData);
            let timeContainer = formData.mainData.timeContainer.split(",")
            if (timeContainer.length > 0 && timeContainer[0] != "") {
                formData.mainData.distributorStartTime = timeContainer[0]
                formData.mainData.distributorEndTime = timeContainer[1]
            } else {
                this.$Message.error("请选择有效起止日期！")
            }
            return true
        },
        getMenu() {
          let params = {
            page: 1,
            rows: 30,
            sort: "createDate",
            order: "desc",
            wheres:
              '[{"name":"distributorType","value":"2006,2004","displayType":"checkbox"}]',
          };
          this.http
            .post("api/DistributorNews/getPageData", params, false)
            .then((reslut) => {
              let vf = this.getOption("directdistriCode");
              vf.data = reslut.rows.map(i => {
                return {
                  key: i.distributorCode,
                  value: i.distributorName
                }
              })
            });
        }
    }
};
export default extension;
